package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.FinanceService;
import cn.itcast.service.cargo.InvoiceService;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;

@Controller
@RequestMapping("cargo/finance")
public class FinanceController extends BaseController {
    @Reference
    private FinanceService financeService;
    @Reference
    private InvoiceService invoiceService;
    @Reference
    private PackingService packingService;
    @Reference
    private ExportService exportService;

    @RequestMapping(path = "list", name = "查询财务")
    public String findAll(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size){
        FinanceExample financeExample = new FinanceExample();
        FinanceExample.Criteria criteria = financeExample.createCriteria();
        PageInfo pageInfo = financeService.findByPage(page,size,financeExample);


        request.setAttribute("page",pageInfo);



        return "cargo/finance/finance-list";
    }

    @RequestMapping(path = "/toAdd", name = "跳转财务添加")
    public String toAdd(String invoiceId) {
        Invoice invoice= invoiceService.findById(invoiceId);
        Finance finance = new Finance();
        finance.setFinanceId(invoiceId);
        finance.setInvoiceId(invoiceId);
        System.out.println(invoice.getInvoiceMoney());
        finance.setInvoiceMoney(invoice.getInvoiceMoney());
        finance.setInvoiceTime(invoice.getInvoiceTime());
        Packing packing = packingService.findById(invoiceId);
        finance.setExportNos(packing.getExportNos());
        Export export = exportService.findById(packing.getExportId());
        System.out.println(export.getShipmentPort());
        finance.setShipmentPort(export.getShipmentPort());
        finance.setDestinationPort(export.getDestinationPort());
        finance.setConsignee(export.getConsignee());

        request.setAttribute("finance", finance);
        return "cargo/finance/finance-add";
    }


    @RequestMapping(value = "edit",name = "财务添加")
    public String edit(Finance finance){
        finance.setCreateBy(getLoginUser().getId());
        finance.setCreateDept(getLoginUser().getDeptId());
        finance.setCreateTime(new Date());
        finance.setCompanyId(getCompanyId());
        finance.setCompanyName(getCompanyName());
        financeService.save(finance);
        return "redirect:/cargo/finance/list.do";
    }

    @RequestMapping(value = "toView",name = "财务添加")
    public String toView(String financeId ){
        Finance finance = financeService.findById(financeId);
        request.setAttribute("finance",finance);
        return "cargo/finance/finance-view";
    }
}
